package com.example.leetdemo.process;

import com.example.leetdemo.utils.DateUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.HashSet;
import java.util.Set;

public class Solution2707 {

    public int minExtraChar(String s, String[] dictionary) {
        if (s.length() == 0) {
            return 0;
        }
        int[] dp = new int[s.length() + 1];
        Set<String> set = new HashSet<>();
        int maxSize = 0;
        for (int i = 0; i < dictionary.length; i++) {
            set.add(dictionary[i]);
            maxSize = Math.max(dictionary[i].length(), maxSize);
        }
        dp[0] = 0;
        dp[1] = set.contains(s.substring(0, 1)) ? 0 : 1;
        for (int i = 1; i < s.length(); i++) {
            int k = 10000007;
            for (int j = i; j >= 0 && i - j <= maxSize + 1; j--) {
                String s1 = s.substring(j, i + 1);
                if (set.contains(s1)) {
                    k = Math.min(dp[j], k);
                    break;
                } else {
                    k = Math.min(dp[j] + i - j + 1, k);
                }
            }
            dp[i + 1] = k;
        }
        return dp[s.length()];
    }

    public static void main(String[] args) {
//        Solution2707 solution2707 = new Solution2707();
//        System.err.println(solution2707.minExtraChar("sayhelloworld", new String[] {"hello", "world"}));
//        System.err.println(solution2707.minExtraChar("leetsscode", new String[] {"leet", "code"}));
        LocalDateTime now = DateUtils.parse("2024-02-01 03:05:00");
        String endTime = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(now) + " 00:00:00";
        String monthBeginTime = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(now
                .minusMonths(1).with(TemporalAdjusters.firstDayOfMonth())) + " 00:00:00";
        System.err.println(endTime);
        System.err.println(monthBeginTime);
    }


}
